﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class login2 : System.Web.UI.UserControl
{
    public Patron patron;
    public Person person;
    public ManagePatron patronInfo;
    public ManagePerson personInfo;
    public ManageEmployee employee;
    string lib;

    protected void Page_Load(object sender, EventArgs e)
    {
        if( !IsPostBack)
        {
            patron = (Patron)Session["Patron"];
            person = (Person)Session["Person"];
            pnlLoggedIn.Visible = true;
        }
        if (patron == null && person == null)
        {
            patron = new Patron();
            person = new Person();
            pnlLoggedIn.Visible = false;
            pnlNotLoggedIn.Visible = true;
        }
        else
        {
            pnlNotLoggedIn.Visible = false;
            pnlLoggedIn.Visible = true;
        }

        patronInfo = new ManagePatron();
        personInfo = new ManagePerson();
        employee = new ManageEmployee();
        lib = Request.QueryString["id"].ToString();
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        int cardNum = Convert.ToInt32(txtCardNumber.Text.ToString());

        int result = patronInfo.login(cardNum, txtPassword.Text.ToString());
        int result2 = employee.isEmployee(cardNum);

        if (result == 0)
            lblMessage.Text = "Invalid login.";
        else
        {
            pnlLoggedIn.Visible = true;
            pnlNotLoggedIn.Visible = false;

            Session["Patron"] = patronInfo.getPatron(cardNum);
            Session["Person"] = personInfo.getPerson(cardNum);

            if (result2 == 1)
                btnEmployeeView.Visible = true;
            else
                btnEmployeeView.Visible = false;
        }
    }

    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        Session["Patron"] = null;
        Session["Person"] = null;
        pnlNotLoggedIn.Visible = true;
        pnlLoggedIn.Visible = false;

        patron = new Patron();
        person = new Person();
        patronInfo = new ManagePatron();
        personInfo = new ManagePerson();
        employee = new ManageEmployee();
    }

    protected void btnMyAccount_Click(object sender, EventArgs e)
    {
        Response.Redirect("PatronView.aspx?id=" + lib);
    }

    protected void btnEmployeeView_Click(object sender, EventArgs e)
    {
        Response.Redirect("LibrarianView.aspx?id=" + lib);
    }
}
